import Dialog from '../../../../miniprogram_npm/vant-weapp/dialog/dialog';
import {WorkModel} from "../../../../models/work.js";
import { formatDate } from "../../../../utils/util.js"
const _WorkModel = new WorkModel();
var _self;
Page({
  data: {
    src: '',
    companyLogo:'https://admin.51imu.com/file/resources/functiontobeopened.png',
    degreeIndex:1,
    dateofEnrollment:'2010-09',
    dateofGraduation:'2014-06',
    positionArr:["产品经历","软件研发"],
    positionIndex:0,
    businessArr:["互联网科技","机械制造","生物可以"],
    businessIndex:0,
    editId:null,
    type:"",
    btnName:"保存"
  },
  upload() {
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success(res) {
        const src = res.tempFilePaths[0]
        wx.navigateTo({
          url: `/pages/profile/upload/upload?src=${src}`
        })
        // wx.redirectTo({
        //   url: `/pages/profile/upload/upload?src=${src}`
        // })
      }
    })
  },
  onLoad(options) {
    _self = this;
    if (options.type && options.type == "edit") {
      let data = JSON.parse(options.current)
      _self.setData({
        btnName: "更新",
        type: options.type,
        company:data.work,
        position:data.title,
        industry:data.trade,
        editId:data.id,
        summary:data.summary,
        dateofEnrollment: formatDate(data.startTime),
        dateofGraduation:formatDate(data.endTime)
      })
    }
  },
  formSubmit:function(e){
    let company = e.detail.value.company;
    let position= e.detail.value.position;
    let industry = e.detail.value.industry;
    let summary = e.detail.value.summary;
    if(company==""){
      Dialog.alert({
        message: "请输入公司名称。"
      });
      return;
    }
    if (position == "") {
      Dialog.alert({
        message: "请输入职位信息。"
      });
      return;
    }

    if (industry == "") {
      Dialog.alert({
        message: "请输入行业信息。"
      });
      return;
    }


    let params = {
      "work": company,
      "trade": industry,
      "title": position,
      "startTime": _self.data.dateofEnrollment +"-01 00:00:00",
      "endTime": _self.data.dateofGraduation + "-01 00:00:00",
      "summary": summary
    }
    if (this.data.type == "edit") {
      params.id = _self.data.editId;
      _WorkModel.updateWork(params, function () {
        var pages = getCurrentPages();
        if (pages.length > 1) {
          var prePage = pages[pages.length - 2];
          prePage.changeData();
          wx.navigateBack();
        }
        return;
      })
    }else{
      _WorkModel.addWork(params, function () {
        var pages = getCurrentPages();
        if (pages.length > 1) {
          var prePage = pages[pages.length - 2];
          prePage.changeData();
          wx.navigateBack();
        }
        return;
      })
    }
 
  },
  enrollDateChange:function(e){
    this.setData({
      dateofEnrollment:e.detail.value
    })
  },
  graduateDateChange:function(e){
    this.setData({
      dateofGraduation: e.detail.value
    })
  }
})
